//
//  NetStatusHandle.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "NetStatusHandle.h"
#import "AFNetworking.h"

@implementation NetStatusHandle

+ (NetStatusHandle *)shareHandle
{
    static NetStatusHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[NetStatusHandle alloc] init];
    });
    return handle;
}

// 保存网络状态
- (void)saveNetStatus:(NSString *)status
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:status forKey:@"netStatus"];
    [myDefault synchronize];
}

// 获取网络状态
- (NSString *)getNetStatus
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"netStatus"];
}


// 判断网络状态
- (NSInteger)judgeTheNet
{
    __block NSInteger number;
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // 开启网络状态监测
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        number = status;
    }];
    return number;
}

// 保存最后一次产品界面搜索的结果
- (void)saveLastResultString:(NSString *)lastString
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:lastString forKey:@"lastResultString"];
    [myDefault synchronize];
}

// 获取最后一次产品界面搜索的结果
- (NSString *)getLastResultString
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"lastResultString"];
}

// 保存当前登陆融云的用户ID
- (void)saveCurrentLoginRongCloudUserID:(NSString *)currentUserID
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:currentUserID forKey:@"currentLoginRongYunUserID"];
}

// 取出当前登陆融云用户的ID
- (NSString *)getCurrentLoginRongCloudUserID
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"currentLoginRongYunUserID"];
}

// 通过提取当前融云用户的ID
- (NSString *)getCurrentLoginRongCloudUserIDByKey:(NSString *)saveKey
{
    NSArray *tempArray = [saveKey componentsSeparatedByString:@"!"];
    return [tempArray lastObject];
}

// 通过key提取当前用户信息对应的发送者ID
- (NSString *)getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:(NSString *)saveKey
{
    NSArray *tempArray = [saveKey componentsSeparatedByString:@"!"];
    return [tempArray firstObject];
}



// 自适应宽度
- (CGFloat)adjustWidth:(NSString *)tempString size:(NSInteger)size height:(NSInteger)shouldHeight
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size * kAdjustParamater] forKey:NSFontAttributeName];
    CGRect frame = [tempString boundingRectWithSize:CGSizeMake(10000, shouldHeight * kAdjustParamater) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
        return frame.size.width;
}

// 自适应高度
- (CGFloat)adjustHeight:(NSString *)tempString size:(NSInteger)size width:(NSInteger)shouleWidth
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size * kAdjustParamater] forKey:NSFontAttributeName];
    CGRect frame = [tempString boundingRectWithSize:CGSizeMake(shouleWidth * kAdjustParamater, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

// 设置行间距
- (NSMutableAttributedString *)setHangJianJvOfLabel:(NSString *)shouldString lineHeight:(NSInteger) gao
{
    NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc] initWithString:shouldString];
    NSMutableParagraphStyle *paragraghStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraghStyle setLineSpacing:gao * kAdjustParamater];
    [tempString addAttribute:NSParagraphStyleAttributeName value:paragraghStyle range:NSMakeRange(0, shouldString.length)];
    return tempString;
}

// 设置行间距并且设置前几个字大写
- (NSMutableAttributedString *)setHangJianJvOfLabel:(NSString *)shouldString lineHeight:(NSInteger) gao QianJieGeDaXie:(NSInteger)count
{
    NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc] initWithString:shouldString];
    NSMutableParagraphStyle *paragraghStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraghStyle setLineSpacing:gao * kAdjustParamater];
    [tempString addAttribute:NSParagraphStyleAttributeName value:paragraghStyle range:NSMakeRange(0, shouldString.length)];
    // 设置前几个字颜色
    [tempString addAttribute:NSForegroundColorAttributeName value:kCyanColor range:NSMakeRange(7, count)];
    // 设置前几个字大写
    [tempString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22 * kAdjustParamater] range:NSMakeRange(7, count)];
    return tempString;
}

@end
